package com.example.sbmpdemo.controller;

import com.example.sbmpdemo.pojo.Schedule;
import com.example.sbmpdemo.service.ScheduleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping
public class SysUserController {

    @Autowired
    private ScheduleService scheduleService;

    @GetMapping("/list")
    public String list(Model model,
                       @RequestParam(required = false) Integer tripModeId){

        List<Schedule> list = scheduleService.selectSysUserCount(tripModeId);

        model.addAttribute("list", list);

        return "list";
    }

    @PostMapping("/add")
    public String add(Schedule schedule){
        int result = scheduleService.add(schedule);

        if (result > 0){
            return "redirect:list";
        } else {
            return "error";
        }
    }

   @GetMapping("/add")
    public String toAdd(){
        return "add";
    }

    @GetMapping("/delete")
    public String delete(Integer id){

        int result = scheduleService.delete(id);

        if (result > 0){
            return "redirect:list";
        } else {
            return "error";
        }
    }


    @GetMapping("/update")
    public String update(Integer id,Integer status){

        Integer update = scheduleService.modify(id,status);

        if (update > 0){
            return "redirect:list"; //重定向
        }
        return "update";
    }

}
